//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

package fms.web.action;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import fms.common.AdminInfo;
import fms.common.UserInfo;
import fms.services.AdminQueryService;
import fms.services.ServiceException;
import fms.services.ServiceManager;
import fms.services.UserQueryService;
import fms.web.WebConst;
import fms.web.form.LoginForm;

/** 
 * MyEclipse Struts
 * Creation date: 11-04-2006
 * 
 * XDoclet definition:
 * @struts.action path="/login" name="loginForm" input="/login.jsp" scope="request" validate="true"
 */
public class LoginAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	private Logger log = Logger.getLogger(LoginAction.class.getName());
	
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		LoginForm loginForm = (LoginForm) form;
		
		String password = "";
		System.out.println("hi1");
		try {
			if(loginForm.getRadio().equals("0")){
			UserQueryService service =(UserQueryService)ServiceManager.getDefaultManager().getService(UserQueryService.class);
			UserInfo user = new UserInfo();
			user=service.getUserInfoByID(loginForm.getId());
			password=user.getUserPass();}
			else{
				AdminQueryService service =(AdminQueryService)ServiceManager.getDefaultManager().getService(AdminQueryService.class);
				AdminInfo admin = new AdminInfo();
				admin=service.getAdminInfoByID(loginForm.getId());
				password=admin.getAdminPass();}
		} catch (ServiceException e) {
			log.log(Level.SEVERE,"user login failed!=> "+e.getMessage());
            request.setAttribute(WebConst.USER_LOGIN_EXCEPTION_STR, e.getMessage());
            return mapping.findForward("login");
		}
		if(loginForm.getRadio().equals("0")){
			if (password.equals(loginForm.getPassword())){
			return (mapping.findForward("loginuser"));
			}
			else{
			return (mapping.findForward("loginfail"));
			}
		}
		else{
			if (password.equals(loginForm.getPassword())){
			return (mapping.findForward("loginadmin"));
			}
			else{
			return (mapping.findForward("loginfail"));
			}
		}
	}

}

